package fl.ass.service.service.impl;

import cmm.user.facade.system.api.ISysUsrApiService;
import cmm.user.facade.system.dto.SysUsrDTO;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.fl.custpay.dto.DataResultDTO;
import cn.fl.finance.api.*;
import cn.fl.finance.constant.FinLoanConstant;
import cn.fl.finance.dto.*;
import cn.fl.project.constant.PrjPrdPaySchMConstant;
import cn.fl.project.dto.PrjPrdPaySchMDTO;
import cn.fl.project.service.IPrjPrdPaySchMApiService;
import fl.ass.facade.condition.AssTrailCarDInfQueryCondition;
import fl.ass.facade.condition.AssTrailCarDQueryCondition;
import fl.ass.facade.dto.*;
import fl.ass.service.service.*;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 借款申请
 * 作者:hongyanbo
 * 时间:2018/10/15
 */
@Service
public class FinLoanApplyServiceImpl implements IFinLoanApplyService {
    @Autowired
    private IFinPayDtlRApiService rApiService;
    @Autowired
    private IFinPayDtlApiService payDtlApiService;
    @Autowired
    private IFinPayApplyInfoApiService finPayApplyInfoApiService;
    @Autowired
    private IFinLoanDtlApiService finLoanDtlApiService;
    @Autowired
    private IFinLoanInfoApiService finLoanInfoApiService;
    @Autowired
    private IAssTrailCarMService assTrailCarMService;
    @Autowired
    private IAssTrailCarDService assTrailCarDService;
    @Autowired
    private ISysUsrApiService usrApiService;
    /**
     * 拖车渠道
     */
    @Autowired
    private IAssTrailCarChannelService carChannelService;
    @Autowired
    private IAssChnBankAccService bankAccService;
    @Autowired
    private IAssTrailCarDInfService assTrailCarDInfService;

    @Autowired
    private IPrjPrdPaySchMApiService prjPrdPaySchMApiService;

    @Override
    public DataResultDTO loanApplyFromFlow(Long id,String orgCd) {
        AssTrailCarMDTO mdto = assTrailCarMService.selectById(id);
        AssTrailCarDQueryCondition qc = new AssTrailCarDQueryCondition();
        qc.setMasterId(id);
        AssTrailCarDDTO ddto = assTrailCarDService.selectOne(qc);
        Long finLoanInfoId = addFinLoanInfo(ddto,orgCd,mdto);
        Long finLoanDtlId = addFinLoanDtl(finLoanInfoId,mdto);
        Long payApplyId = addPayApply(id,finLoanInfoId, mdto);
        AssTrailCarDInfQueryCondition cc = new AssTrailCarDInfQueryCondition();
        cc.setMasterId(mdto.getId());
        List<AssTrailCarDInfDTO> assTrailCarDInfDTOS = assTrailCarDInfService.searchList(cc);
        if(assTrailCarDInfDTOS.size()>1){
            throw new BaseException("数据错误：ass_trail_car_d_inf 有多笔数据:" + mdto.getId());
        }
        AssTrailCarDInfDTO assTrailCarDInfDTO = assTrailCarDInfDTOS.get(0);
        Long payDtlId = addPayDtl(payApplyId,finLoanDtlId,mdto,assTrailCarDInfDTO.getId());
        // 绑定资产
        addFinPayDtlRDTO(assTrailCarDInfDTO.getId(),mdto,ddto);
        return null;
    }

    private void addFinPayDtlRDTO(Long payDtlId, AssTrailCarMDTO mdto,AssTrailCarDDTO ddto) {
        FinPayDtlRDTO dto = new FinPayDtlRDTO();
        dto.setPayDtlId(payDtlId);
        Long paySchId = ddto.getPaySchId();
        List<PrjPrdPaySchMDTO> prjPrdPaySchMDTOS = this.prjPrdPaySchMApiService.searchTheSamePrdOtherPaySchMList(paySchId);
        if (CollectionUtils.isNotEmpty(prjPrdPaySchMDTOS)) {
            for (PrjPrdPaySchMDTO prjPrdPaySchMDTO : prjPrdPaySchMDTOS) {
                if (PrjPrdPaySchMConstant.PAY_SCH_TYP_CD_WS.equals(prjPrdPaySchMDTO.getPaySchTypCd())) {
                    paySchId = prjPrdPaySchMDTO.getId();
                    break;
                }
            }
        }
        dto.setPaySchId(paySchId);
        dto.setPayAmt(mdto.getBackAmt());
        dto.setRem("本数据来自拖车申请流程");
        rApiService.add(dto);
    }

    private Long addPayDtl(Long payApplyId, Long finLoanDtlId, AssTrailCarMDTO mdto, Long assTrailCarDInfId) {
        FinPayDtlDTO dtlVO = new FinPayDtlDTO();
        dtlVO.setId(assTrailCarDInfId);
        dtlVO.setPayAmt(mdto.getBackAmt());
        dtlVO.setLoanDtlId(finLoanDtlId);
        dtlVO.setPayApplyId(payApplyId);
        AssChnBankAccDTO bankAccDTO = this.bankAccService.getByChannelId(mdto.getChannelId());
        if(bankAccDTO!=null){
            dtlVO.setBkId(bankAccDTO.getId());
            dtlVO.setRcptUnt(bankAccDTO.getAccNm());
            dtlVO.setAccNo(bankAccDTO.getAccNo());
            dtlVO.setPayeeBkNm(mdto.getPayeeBankNm());
            dtlVO.setPayeeBkNo(bankAccDTO.getBkNo());
            dtlVO.setCrtTm(new Date());
            dtlVO.setCrtUsrId(mdto.getCrtUsrId());
            String accTypCd = bankAccDTO.getAccTypCd();
            if(StringUtils.isNotBlank(accTypCd)){
                if(accTypCd.startsWith("01")){
                    dtlVO.setBkAccTypCd("0");
                }else{
                    dtlVO.setBkAccTypCd("1");
                }
            }else{
                throw new BaseException("银行卡信息当中没有对公对私信息。");
            }
        }else{
            throw new BaseException("未查到银行卡数据");
        }
        // FIXME addWithId
        return payDtlApiService.addWithId(dtlVO);
    }

    /**
     * 添加付款申请
     */
    private Long addPayApply(Long id,Long infoId, AssTrailCarMDTO mdto) {
        FinPayApplyInfoDTO applyInfoVO = new FinPayApplyInfoDTO();
        applyInfoVO.setId(id);
        applyInfoVO.setLoanId(infoId);
//            applyInfoVO.setLoanNo();
        applyInfoVO.setPayAmt(mdto.getBackAmt());

        applyInfoVO.setPayStsCd(FinLoanConstant.FIN_PAY_STS_CD_1);
        applyInfoVO.setCrtTm(new Date());
        applyInfoVO.setCrtUsrId(mdto.getApplyId());
        applyInfoVO.setLoanTypCd("2");// 表示数据来自拖车申请流程
        finPayApplyInfoApiService.addWithId(applyInfoVO);
        return id;
    }
    private Long addFinLoanDtl(Long finLoanInfoId, AssTrailCarMDTO mdto) {
        FinLoanDtlDTO dto = new FinLoanDtlDTO();
        dto.setLoanId(finLoanInfoId);
        dto.setLoanAmt(mdto.getBackAmt());
        dto.setLoanTypCd(FinLoanConstant.FEE_TYP_PULL_CAR_CD);
        dto.setLoanRem("数据来自拖车申请流程");
        dto.setPayTypCd("1");
        return finLoanDtlApiService.add(dto);
    }

    private Long addFinLoanInfo(AssTrailCarDDTO ddto,String orgCd,AssTrailCarMDTO mdto) {
        FinLoanInfoDTO finLoanInfoVO = new FinLoanInfoDTO();
        finLoanInfoVO.setBuOrgCd(ddto.getBuOrgCd());
        finLoanInfoVO.setCstMgrOrgCd(orgCd);
        finLoanInfoVO.setCstMgrNm(mdto.getApplyNm());
        finLoanInfoVO.setCstMgrId(mdto.getApplyId());
        finLoanInfoVO.setLoanStsCd(FinLoanConstant.FIN_LOAN_STS_CD_0);
        finLoanInfoVO.setLoanAmt(mdto.getBackAmt());
        finLoanInfoVO.setLoanTm(new Date());
        SysUsrDTO sysUsrDTO = this.usrApiService.selectSysUsrById(mdto.getApplyId(),"11000",orgCd);
        if(sysUsrDTO==null){
            throw new BaseException("查询用户失败usrId:"+ddto.getCstMgrId() + "  orgCd:" + orgCd);
        }
        finLoanInfoVO.setCstMgrCd(sysUsrDTO.getUsrNm());
        return finLoanInfoApiService.add(finLoanInfoVO);
    }
}
